package Server;

import java.io.*;
import java.net.Socket;

import Interface.ServerColleague;

/**
 * Concrete Collegue
 * Object storing information for one connected socket 
 * @author Yaxing Chen
 *
 */
class SocketHandler implements ServerColleague{
	private Socket client;
	private DataInputStream dis;
	private DataOutputStream dos;
	private int socketId;
	
	private ChatMediator mediator;
	
	SocketHandler(Socket client, DataInputStream dis, DataOutputStream dos, int id){
		this.client = client;
		this.dis = dis;
		this.dos = dos;
		this.socketId = id;
		this.mediator = Server.getMediator();
	}
	
	public Socket getSocket(){
		return client;
	}
	
	public DataInputStream getInputStream(){
		return dis;
	}
	
	public DataOutputStream getOutputStream(){
		return dos;
	}
	
	public int getId(){
		return socketId;
	}
	

	/**
	 *  accept input from socket--get from receiver
	 * and send to mediator for further analysis.
	 * @param content
	 */
	public boolean accept(String content){
		boolean result = mediator.analyze(socketId, content);
		if(!result){
			try{
				dis.close();
				dos.close();
			}catch(IOException e){
				
			}
		}
		return result;
	}
	
	/**
	 * write content to socket
	 * @param content
	 * @return
	 */
	public boolean notify(String content){
		try{
			getOutputStream().writeUTF(content);
		}catch(IOException e){
			return false;
		}
		return true;
	}
}